package com.tisson.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.tisson.database.FormatUtil;
import com.tisson.database.StatementUtil;
import com.tisson.pojo.AcDepartment;
import com.tisson.pojo.AdminDepart;

public class AdminDepartDAO {
	private String query;
	private ResultSet rs;
	private Statement stmt;

	public AdminDepart findById(String id) throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "select * from admin_depart where id = '" + id + "'";
		rs = stmt.executeQuery(query);
		AdminDepart ad = new AdminDepart();
		while (rs.next()) {
			ad.setId(FormatUtil.ConvertUuid(rs.getBytes("id")));
			ad.setAdminId(FormatUtil.ConvertUuid(rs.getBytes("admin_id")));
			ad.setDepartId(FormatUtil.ConvertUuid(rs.getBytes("depart_id")));
		}
		rs.close();
		stmt.close();
		return ad;
	}

	public List findByAdminId(String id) throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "select * from admin_depart where admin_id = '" + id + "'";
		rs = stmt.executeQuery(query);
		List list = new ArrayList();
		while (rs.next()) {
			AdminDepart ad = new AdminDepart();
			ad.setId(FormatUtil.ConvertUuid(rs.getBytes("id")));
			ad.setAdminId(FormatUtil.ConvertUuid(rs.getBytes("admin_id")));
			ad.setDepartId(FormatUtil.ConvertUuid(rs.getBytes("depart_id")));
			list.add(ad);
		}
		rs.close();
		stmt.close();
		return list;
	}

	public List findAll() throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "select * from admin_depart";
		rs = stmt.executeQuery(query);
		List list = new ArrayList();
		while (rs.next()) {
			AdminDepart ad = new AdminDepart();
			ad.setId(FormatUtil.ConvertUuid(rs.getBytes("id")));
			ad.setAdminId(FormatUtil.ConvertUuid(rs.getBytes("admin_id")));
			ad.setDepartId(FormatUtil.ConvertUuid(rs.getBytes("depart_id")));
			list.add(ad);
		}
		rs.close();
		stmt.close();
		return list;
	}

	public void update(AdminDepart ad) throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "update admin_depart set " + "admin_id = '" + ad.getAdminId()
				+ "',depart_id = '" + ad.getDepartId() + "'" + " where id = '"
				+ ad.getId() + "'";
		stmt.executeUpdate(query);
		stmt.close();
	}

	public void save(AdminDepart ad) throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "insert into admin_depart(admin_id, depart_id) values('"
				+ ad.getAdminId() + "','" + ad.getDepartId() + "')";
		stmt.executeUpdate(query);
		stmt.close();
	}

	public void delete(AdminDepart ad) throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "delete from admin_depart where id = '" + ad.getId() + "'";
		stmt.executeUpdate(query);
		stmt.close();
	}

}
